package com.sword.erp.stock.config;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis配置
 * @author sword
 * @date 2021/10/30 16:20
 */
@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    /**
     * 注册redis访问对象
     * @param factory
     * @return org.springframework.data.redis.core.RedisTemplate<java.lang.String,java.lang.Long>
     * @author sword
     * @date 2021/10/30 16:47
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {

        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(factory);

        // 如果不设置序列化类则“org.springframework.data.redis.core.ValueOperations.decrement(K)”方法会报错
        redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Object.class));
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        return redisTemplate;
    }
}
